<template>
  <view class="container">
    <!-- @click可以给原生的dom对象注册事件  MySearch不是原生dom对象, 是一个组件 -->
    <!-- @子组件派发的事件名="自己自定义的事件名" -->
    <!-- 
      1. 子传父 派发的事件名 click 
      2. .native 修饰符 给组件内部的根元素去注册事件

     -->
    <view class="search-box">
      <MySearch
        @click.native="goSearch"
        bgColor="skyblue"
        bdRadius="10rpx"
      ></MySearch>
    </view>
    <!-- 轮播图区域 -->
    <swiper class="swiper" indicator-dots :interval="2000" autoplay circular>
      <swiper-item v-for="item in swiperList" :key="item.goods_id">
        <!-- 声明式 navigator 编程式 注册事件 @click="" -->
        <navigator
          :url="`/pages_sub1/product-detail/product-detail?pid=${item.goods_id}`"
          class="swiper-item"
        >
          <image :src="item.image_src" />
        </navigator>
      </swiper-item>
    </swiper>
    <!-- 分类列表区域 -->
    <view class="nav-list">
      <view
        class="nav-item"
        @click="goNavItem(item)"
        v-for="(item, index) in navList"
        :key="index"
      >
        <image :src="item.image_src" class="nav-img"></image>
      </view>
    </view>
    <!-- 楼层区域 -->
    <view class="floor-list">
      <!-- 楼层 item 项 -->
      <view class="floor-item" v-for="(item, index) in floorList" :key="index">
        <!-- 楼层标题 -->
        <image :src="item.floor_title.image_src" class="floor-title"></image>
        <!-- 楼层图片区域 -->
        <view class="floor-img-box">
          <!-- 左侧 1 个大图 -->
          <view class="left-img-box">
            <image
              :src="item.product_list[0].image_src"
              :style="{ width: item.product_list[0].image_width + 'rpx' }"
              mode="widthFix"
              @click="goToProductList(item.product_list[0])"
            ></image>
          </view>

          <!-- 右侧 4 个小图 -->
          <view class="right-img-box">
            <block v-for="(product, idx) in item.product_list" :key="idx">
              <view class="right-img-item" v-if="idx !== 0">
                <image
                  :src="product.image_src"
                  :style="{ width: product.image_width + 'rpx' }"
                  mode="widthFix"
                  @click="goToProductList(product)"
                ></image>
              </view>
            </block>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import MySearch from "@/components/my-search"
import badge from '@/mixins/badge.js'
export default {
  mixins: [badge], 
  components: {
    MySearch,
  },
  data() {
    return {
      swiperList: [], // 轮播图列表
      navList: [], // 分类列表
      floorList: [], // 楼层的数据列表
    }
  },
  created() {
    // 轮播图
    this.getSwiperList()
    // 分类
    this.getNavList()
    // 楼层
    this.getFloorList()
  },
  methods: {
    // 跳转到搜索页面
    goSearch() {
      uni.navigateTo({
        url: "/pages_sub1/search/search",
      })
    },
    // 跳转到商品列表
    goToProductList(product) {
      const navigatorUrl = product.navigator_url
      // /pages/goods_list?query=服饰
      const query = navigatorUrl.split("?")[1]
      uni.navigateTo({ url: "/pages_sub1/product-list/product-list?" + query })
    },
    // 跳转到分类tab
    goNavItem(item) {
      if (item.name === "分类") {
        uni.switchTab({
          url: "/pages/category/category",
        })
      }
    },
    async getFloorList() {
      const {
        message,
        meta: { status },
      } = await this.$http.get("/home/floordata")
      if (status !== 200) return this.$showMsg()
      this.floorList = message
    },
    async getNavList() {
      const {
        message,
        meta: { status },
      } = await this.$http.get("/home/catitems")
      if (status !== 200) return this.$showMsg()
      this.navList = message
    },
    async getSwiperList() {
      const {
        message,
        meta: { status },
      } = await this.$http.get("/home/swiperdata")
      if (status !== 200) {
        // return uni.showToast({
        //   title: '数据获取失败',
        //   icon: 'none'
        // });
        return this.$showMsg()
      }
      // 请求成功
      this.swiperList = message
    },
  },
}
</script>

<style scoped lang="scss">
.search-box {
  position: sticky;
  top: 0;
  left: 0;
  z-index: 999;
}
.swiper-item,
image {
  width: 100%;
  height: 100%;
}
.nav-list {
  display: flex;
  justify-content: space-around;
  margin: 30rpx 0;

  .nav-img {
    width: 128rpx;
    height: 140rpx;
  }
}
.floor-title {
  display: flex;
  width: 100%;
  height: 60rpx;
}
.right-img-box {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-around;
}

.floor-img-box {
  display: flex;
  padding-left: 10rpx;
}
</style>